﻿using CommunityToolkit.Mvvm.Messaging;
using Company.Core.Logger;
using Company.Shell.Dependencylnjection;
using Company.Shell.Models;
using Company.Shell.Views;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace Company.Shell
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public new static App Current => (App)Application.Current;
        public IServiceProvider Services { get; private set; } =ServiceManager.Load();

        protected override void OnStartup(StartupEventArgs e)
        {
            // 记录应用程序启动日志
            Logs.Info("应用程序启动");

            var shellView = Services.GetService<ShellView>();
            if (shellView != null)
            {
                shellView.Show();
            }
            else
            {
                Logs.Error("ShellView 未能从服务容器中获取，应用程序无法正常启动。");
                Shutdown();
            }
        }

        protected override void OnExit(ExitEventArgs e)
        {
            // 记录应用程序退出日志
            Logs.Info("应用程序正常退出");
            WeakReferenceMessenger.Default.Send(new OnExitMessage("善后工作"));
            base.OnExit(e);
        }
    }
}
